﻿using DotNet.Data;
using DotNet.Utility;
using System;
using System.Linq;
using System.Collections.Generic;
using XNWebModel;

namespace XNWebService
{
    /// <summary>
    /// 
    /// </summary>
    public class CustomerServices
    {
        private readonly Repository<CustomerService> dao = new Repository<CustomerService>(new SiteDatabase());

        /// <summary>
        /// 新增客户服务[投诉建议]
        /// </summary>
        /// <param name="entity">客户服务实体</param>
        /// <returns>执行成功返回BoolMessage.True</returns>
        public BoolMessage AddCustomer(CustomerService entity)
        {
            entity.ServiceCategory = "意见建议";
            entity.ServiceCategoryId = 1;
            entity.CreateDateTime = DateTime.Now;

            if (string.IsNullOrEmpty(entity.Account))
            {
                entity.Account = entity.ContactPhone;
            }

            try
            {
                dao.Insert(entity);
                return BoolMessage.True;
            }
            catch (Exception e)
            {
                return new BoolMessage(false, e.Message);
            }
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="account">联系电话</param>
        /// <returns>contactphone为null或空时,返回全部投诉建议数据</returns>
        public BoolMessage<List<CustomerService>> GetCustomerService(string account)
        {
            List<CustomerService> model = null;
            if (string.IsNullOrEmpty(account))
            {
                model = dao.Fetch(p => p.ServiceCategoryId == 1);
                model.Sort((x, y) => { return -x.CreateDateTime.CompareTo(y.CreateDateTime); });
            }
            else
            {
                model = dao.Fetch(p => p.ServiceCategoryId == 1 && p.Account == account);
                model.Sort((x, y) => { return -x.CreateDateTime.CompareTo(y.CreateDateTime); });
            }

            if (model == null)
            {
                return new BoolMessage<List<CustomerService>>(false, "未查询到相应数据");
            }
            return new BoolMessage<List<CustomerService>>(model);
        }

        /// <summary>
        /// 新增客户服务[2寻物启事，3失物招领]
        /// </summary>
        /// <param name="entity">客户服务实体</param>
        /// <returns>执行成功返回BoolMessage.True</returns>
        public BoolMessage AddLookGetBack(CustomerService entity)
        {
            #region   ==========  参数检测  ==========
            if (entity == null)
            {
                return new BoolMessage(false, "传入参数有误，请检查参数名称、类型。");
            }

            if (string.IsNullOrEmpty(entity.Title))
            {
                return new BoolMessage(false, "传入参数标题为空。");
            }
            else
            {
                //敏感词检测，若存在则提交失败
                if (SensitiveWord.CheckWords(entity.Title))
                {
                    return new BoolMessage(false, "传入参数标题中含有敏感词！。");
                }
            }

            if (entity.ServiceCategoryId == 0)
            {
                return new BoolMessage(false, "传入参数分类主键为空。");
            }

            if (string.IsNullOrEmpty(entity.Details))
            {
                return new BoolMessage(false, "传入参数详情描述为空。");
            }
            else
            {
                //敏感词检测，若存在则提交失败
                if (SensitiveWord.CheckWords(entity.Details))
                {
                    return new BoolMessage(false, "传入参数详情描述中含有敏感词！。");
                }
            }

            if (string.IsNullOrEmpty(entity.ContactName))
            {
                return new BoolMessage(false, "传入参数联系人为空。");
            }
            else
            {
                //敏感词检测，若存在则提交失败
                if (SensitiveWord.CheckWords(entity.ContactName))
                {
                    return new BoolMessage(false, "传入参数联系人中含有敏感词！。");
                }
            }

            if (string.IsNullOrEmpty(entity.ContactPhone))
            {
                return new BoolMessage(false, "传入参数联系电话为空。");
            }

            if (string.IsNullOrEmpty(entity.Account))
            {
                return new BoolMessage(false, "传入参数账号为空。");
            }

            if (entity.ServiceCategoryId == 2)
            {
                entity.ServiceCategory = "寻物启事";
            }
            else if (entity.ServiceCategoryId == 3)
            {
                entity.ServiceCategory = "失物招领";
            }
            else
            {
                return new BoolMessage(false, "传入参数分类主键有误。");
            }

            if (!string.IsNullOrEmpty(entity.ContactAddress))
            {
                //敏感词检测，若存在则提交失败
                if (SensitiveWord.CheckWords(entity.ContactAddress))
                {
                    return new BoolMessage(false, "传入参数联系地址中含有敏感词！。");
                }
            }
            #endregion ==========   ==========

            entity.CreateDateTime = DateTime.Now;

            try
            {
                dao.Insert(entity);
                return BoolMessage.True;
            }
            catch (Exception e)
            {
                return new BoolMessage(false, e.Message);
            }
        }

        /// <summary>
        /// 查询寻物招领分页
        /// </summary>
        /// <param name="eachPage">每页数量</param>
        /// <param name="pageNum">页码</param>
        /// <param name="serviceCategoryId">分类主键【2寻物启事，3失物招领】</param>
        /// <returns>serviceCategoryId为null或空时,返回全部2寻物启事，3失物招领数据</returns>
        public BoolMessage<List<CustomerService>> GetLookGetBack(int eachPage = 10, int pageNum = 1, int? serviceCategoryId = null)
        {
            List<CustomerService> model = null;
            if (serviceCategoryId == null)
            {
                model = dao.Fetch(p => p.ServiceCategoryId == 2 || p.ServiceCategoryId == 3);
                model.Sort((x, y) => { return -x.CreateDateTime.CompareTo(y.CreateDateTime); });
            }
            else
            {
                model = dao.Fetch(p => p.ServiceCategoryId == serviceCategoryId);
                model.Sort((x, y) => { return -x.CreateDateTime.CompareTo(y.CreateDateTime); });
            }

            if (model == null)
            {
                return new BoolMessage<List<CustomerService>>(false, "未查询到相应数据");
            }

            //分页
            model = model.Skip((pageNum - 1) * eachPage).Take(eachPage).ToList();

            return new BoolMessage<List<CustomerService>>(model);
        }

    }

}
